package edu.imu.question.service.impl;

import edu.imu.question.dto.QuestionnaireTemplateDTO;
import edu.imu.question.dto.TemplateQuestionDTO;
import edu.imu.question.entity.Template;
import edu.imu.question.mapper.TemplateMapper;
import edu.imu.question.service.TemplateQuestionService;
import edu.imu.question.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class TemplateServiceImpl implements TemplateService {

    private TemplateMapper templateMapper;
    private TemplateQuestionService questionService;

    @Autowired
    public TemplateServiceImpl(TemplateMapper templateMapper, TemplateQuestionService questionService) {
        this.templateMapper = templateMapper;
        this.questionService = questionService;
    }

    public List<Template> getAll() {
        return templateMapper.findAll();
    }

    @Override
    public List<Template> getTypeId(long typeId) {
        return templateMapper.findByTypeId(typeId);
    }

    @Override
    public QuestionnaireTemplateDTO getTemplateDetail(long templateId) {
        Template qt = templateMapper.find(templateId);
        QuestionnaireTemplateDTO dto = null;
        if(qt != null) {
            dto = new QuestionnaireTemplateDTO();
            dto.setId(templateId);
            dto.setName(qt.getName());
            dto.setAuthorId(qt.getAuthorId());
            dto.setCreateDate(qt.getCreateDate());
            dto.setPublishDate(qt.getPublishDate());
            dto.setQuestionCount(qt.getQuestionCount());
            dto.setTypeId(qt.getTypeId());
            List<TemplateQuestionDTO> questionDTO = questionService.getDetailByTemplate(templateId);
            dto.setQuestions(questionDTO);
        }
        return dto;
    }

}
